let express = require('express');
const db = require('../../libs/db.js');
const common = require('../../libs/common.js');
module.exports = function(){
    let loginRouter = express.Router();
    loginRouter.get('/',function(req,res){
        res.render('login/reg.ejs',{});
    })
    loginRouter.post('/',function(req,res){
        let username = req.body.username;
        let password = req.body.password;
        if(!username || !password){
            res.status(400).send('用户名密码不能为空').end();
        }else{
            let md5pwd = common.md5(password);
            let sql = `INSERT INTO blog_user (username,password) VALUES ('${username}','${md5pwd}')`;
            db.query(sql).then((data)=>{
                res.redirect('/admin/login');
            }).catch((err)=>{
                res.status(500).send('数据库错误').end();
            })
        }
    })
    return loginRouter;
}
